﻿using System;
using System.Text;
using System.Web;
using Dexter.Dependency;
using Dexter.Domain.Model;
using Dexter.PluginManager;
using Dexter.Plugins.SelectiveTwitter.Jobs;
using Dexter.Scheduler;
using Dexter.Services;
using Dexter.Web.Mvc.ViewModel.ViewsData;
using Dexter.Web.Routing;

namespace Dexter.Plugins.Linkflood {
	public class SelectiveTwitterPlugin : IPlugin {
		readonly IPageService pageService;
		readonly IRoutingService routingService;
		readonly Configuration configuration = ConfigurationHelper.GetConfiguration;

		public SelectiveTwitterPlugin(IPageService pageService, IRoutingService routingService) {
			this.pageService = pageService;
			this.routingService = routingService;
		}

		#region IRenderPlugin Members

		public void Setup() {
			StringBuilder sb = new StringBuilder();
			sb.AppendFormat("<h1>{0}</h1><br />", configuration.PageTitle);
			sb.Append("<ul></ul>");

			Page p = Page.CreateNewPage(configuration.PageTitle, sb.ToString(), configuration.PageSlug);
			p.PublishDate = DateTime.Now;
			p.Publish();
			pageService.Save(p);
		}

		public object GetConfiguration() {
			return ConfigurationHelper.GetConfiguration;
		}

		public void RegisterServices(IDexterContainer container)
		{
			container.Register<IJob, TwitterGrabJob>(TwitterGrabJob.UNIQUE_ID, LifeCycle.Singleton);
			container.Register<IJob, TwitterPosterJob>(TwitterPosterJob.UNIQUE_ID, LifeCycle.Singleton);

			routingService.UpdateRoutes();
		}

		public void ChangeModel(HttpContextBase context, PublicBaseViewData baseViewData) {}

		#endregion
	}
}